﻿/*
        Copyright (C) 2007  Aaron Defazio

        This program is free software; you can redistribute it and/or
        modify it under the terms of the GNU General Public License
        as published by the Free Software Foundation; either version 2
        of the License, or (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using AppLn;
using System.Windows.Media.Imaging;
using System.Drawing;

namespace BlinkLn
{
	public partial class MainWindow
	{
        bool Opening;   //DoubleClick stuff, we only want to do something on first click
        Controller _Cn;

        internal Controller Cn
        {
            get { return _Cn; }
            set { _Cn = value; }
        } 

		public MainWindow()
		{
			this.InitializeComponent();        
		}

        public MainWindow(Controller cn)
        {
            this.InitializeComponent();
            this.Cn = cn;
            Cn.SearchFinished += new Controller.DSearch(Cn_SearchFinished);
            Cn_SearchFinished();
        }

        private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Cn.InputChanged(SearchBox.Text);
        }

        void Cn_SearchFinished()
        {
            Binding ResultBinding = new Binding();
            ResultBinding.Source = Cn.ResultOptions;
            ResultBox.SetBinding(ListBox.ItemsSourceProperty, ResultBinding);
            ResultBox.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(ResultBox_MouseDoubleClick);
        }

        void ResultBox_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!Opening)
                OpenSelected();
        }


        private void OpenSelected()
        {
            Opening = true;
            string key;
            if (Cn.ResultOptions.Count != 0)
            {
                key = Cn.ResultOptions[ResultBox.SelectedIndex].Key;
            }
            else
                key = "";

            Cn.Open(key);
            this.HideInSysTray();
        }

        private void HideInSysTray()
        {
            this.Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
        }

        private void SearchBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {            
            if (e.Key == System.Windows.Input.Key.Enter)
                OpenSelected();
            if (e.Key == System.Windows.Input.Key.Escape)
                this.HideInSysTray();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Activate();
            this.Focus();
            SearchBox.Focus();
        }


	}

    class IconConverter : IValueConverter
    {
        #region IValueConverter Members
        /// <summary>
        /// Code borrowed from http://infosysblogs.com/microsoft/2007/04/wpf_assigning_icon_to_image_co.html
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                //Convert icon to an image
                System.Drawing.Icon cicon = (System.Drawing.Icon)value;
                Bitmap bmp = cicon.ToBitmap();

                MemoryStream strm = new MemoryStream();
                bmp.Save(strm, System.Drawing.Imaging.ImageFormat.Png);
                strm.Seek(0, SeekOrigin.Begin);

                PngBitmapDecoder pbd = new PngBitmapDecoder(strm, BitmapCreateOptions.None, BitmapCacheOption.Default);

                return pbd.Frames[0];
            }
            else return null;


        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new Exception("Can not convert back!?!");
        }

        #endregion
    }
}